<?php
/**
 * Created by PhpStorm.
 * User: Lyq
 * Date: 2018/8/22
 * Time: 上午8:21
 */
namespace app\admin\controller;

use think\Exception;
use think\Db;

class Bussiness extends Common
{
    //商会列表
    public function index()
    {
        $this->templateTitle('商会列表');

        //判断条件
        $where['is_valid']=1;

        //搜索条件
        $condition = request()->param("condition");
        $this->assign("condition", $condition);
        if ($condition)
        {
            $where["title"] = ["LIKE", "%$condition%"];
        }

        $pro = trim(input('pro'));
        $this->assign('area_province',$pro);
        if($pro){
            $where['pro'] = $pro;
        }

        $city = trim(input('city'));
        $this->assign('area_city',$city);
        if($city){
            $where['city'] = $city;
        }

        //获取商会列表
        $list = Db::name("bussiness")
            ->where($where)
            ->order("display_order DESC,update_time DESC")
            ->paginate(10);
        $this->assign("list", $list);

        return $this->fetch();
    }

    //添加新闻
    public function add()
    {
        if (request()->isGet())
        {
            $this->layoutModal();

            return $this->fetch();
        }

        if (request()->isAjax())
        {
            //创建数据
            $data = array(
                "is_valid"    =>  1,
                "create_time" => now_datetime(),
                "update_time" => now_datetime(),
            );
            $data['pro'] = request()->param("pro");
            if(empty($data['pro'])){
                return $this->ajaxError('请选择省份');
            }
            $data['city'] = request()->param("city");
            if(empty($data['city'])){
                return $this->ajaxError('请选择城市');
            }

            $account = request()->param('account');
            if(empty($account)){
                return $this->ajaxError('请输入商会后台登录账号');
            }
            $count = Db::name("bussiness")
                ->where("account", $account)
                ->count();
            if ($count)
            {
                $this->ajaxError("【" . $account . "】" . '商会后台登录账号已存在');
            }
            $data['account'] = $account;
            $password = request()->param('password');
            if(empty($password)){
                return $this->ajaxError('请输入商会后的登录密码');
            }

            $random = rand(100000, 999999);
            $data['salt'] = $random;
            $data['password'] = md5($random . $password . $random);

            //新闻名称
            $data["title"] = request()->param("title");
            if(empty($data['title'])){
                return $this->ajaxError('请输入商会名称');
            }

            $count = Db::name("bussiness")
                ->where("title", $data["title"])
                ->count();
            if ($count)
            {
                $this->ajaxError("【" . $data["title"] . "】" . '商会名称已存在');
            }

            //新闻缩略图
            $data["avatar"] = request()->param("thumb");
            if(empty($data['avatar'])){
                return $this->ajaxError('请上传logo');
            }
            //联系电话
            $data["tel"] = request()->param("tel");
            //详细地址
            $data["address"] = request()->param("address");
            //排序
            $data["display_order"] = request()->param("order");

            //是否首页推荐
            $data["is_recommend"] = request()->param("recommend");
            //简介
            $detail = request()->param("article_detail");
            $data["detail"] = $detail;

            try
            {
                $save = Db::name("bussiness")->insert($data);
                if($save){
                    $this->ajaxSuccess('商会添加成功');
                }else{
                    $this->ajaxError('服务器繁忙，请稍后再试');
                }
            }
            catch (Exception $exception)
            {
                $this->ajaxError($exception->getMessage());
            }
        }
    }

    //编辑新闻
    public function edit()
    {
        //新闻ID
        $articleId = request()->param("id");

        if (request()->isGet())
        {
            $this->layoutModal();

            //获取新闻信息
            $article = Db::name("bussiness")
                ->where("id", $articleId)
                ->find();
            $this->assign("article", $article);

            //处理缩略图
            $this->assign("single_image", array("thumb"=>$article["avatar"]));
            //处理详细内容
            $this->assign("ueditor_content", array("article_detail"=>$article["detail"]));
            $this->assign("area_province", $article['pro']);
            $this->assign("area_city", $article['city']);

            return $this->fetch();
        }

        if (request()->isAjax())
        {
            //
            //创建数据
            $data = array(
                "update_time" => now_datetime(),
            );
            //分类ID
            $data['pro'] = request()->param("pro");
            if(empty($data['pro'])){
                return $this->ajaxError('请选择省份');
            }
            $data['city'] = request()->param("city");
            if(empty($data['city'])){
                return $this->ajaxError('请选择城市');
            }

            $account = request()->param('account');
            if(empty($account)){
                return $this->ajaxError('请输入商会后台登录账号');
            }
            $count = Db::name("bussiness")
                ->where('id', 'NEQ', $articleId)
                ->where("account", $account)
                ->count();
            if ($count)
            {
                $this->ajaxError("【" . $account . "】" . '商会后台登录账号已存在');
            }
            $data['account'] = $account;
            $password = request()->param('password');
            if(!empty($password)){
                $random = rand(100000, 999999);
                $data['salt'] = $random;
                $data['password'] = md5($random . $password . $random);
            }

            //新闻名称
            $data["title"] = request()->param("title");
            if(empty($data['title'])){
                return $this->ajaxError('请输入商会名称');
            }
            $count = Db::name("bussiness")
                ->where('id', 'NEQ', $articleId)
                ->where("title", $data['title'])
                ->count();
            if ($count)
            {
                $this->ajaxError("【" . $data['title'] . "】" . '商会名称已存在');
            }
            //新闻缩略图
            $data["avatar"] = request()->param("thumb");
            if(empty($data['avatar'])){
                return $this->ajaxError('请上传logo');
            }

            //联系电话
            $data["tel"] = request()->param("tel");
            //详细地址
            $data["address"] = request()->param("address");
            //排序
            $data["display_order"] = request()->param("order");

            //是否首页推荐
            $data["is_recommend"] = request()->param("recommend");
            //简介
            $detail = request()->param("article_detail");
            $data["detail"] = $detail;


            try
            {
                $save = Db::name("bussiness")
                    ->where("id", $articleId)
                    ->update($data);
                if($save){
                    //商会城市切换同时更新新闻
                    $change['pro'] = $data['pro'];
                    $change['city'] = $data['city'];
                    Db::name('content_article')
                        ->where("bussiness_id", $articleId)
                        ->update($change);

                    $this->ajaxSuccess('商会编辑成功');
                }else{
                    $this->ajaxError('商会编辑失败');
                }
            }
            catch (Exception $exception)
            {
                $this->ajaxError($exception->getMessage());
            }
        }
    }

    //推荐新闻
    public function recommend()
    {
        if (request()->isAjax())
        {
            //新闻ID
            $articleId = request()->param("aid");
            //推荐状态
            $status = request()->param("status");

            //更新新闻
            Db::name("bussiness")
                ->where("id", $articleId)
                ->update(array(
                    "is_recommend" => $status,
                    "update_time" => now_datetime(),
                ));

            $this->ajaxSuccess('推荐状态修改成功');
        }
    }

    //删除新闻
    public function remove()
    {
        if (request()->isAjax())
        {
            //商会ID
            $id = request()->param("id");

            try
            {
                //更新新闻
                $del = Db::name("bussiness")
                    ->where("id", $id)
                    ->update(['is_valid'=>0]);

                if($del){
                    $this->ajaxSuccess('商会删除成功');
                }else{
                    $this->ajaxError('商会删除失败');
                }
            }
            catch (Exception $exception)
            {
                $this->ajaxError($exception->getMessage());
            }
        }
    }


}